//
//  VisitorView.swift
//  MicroBlog
//
//  Created by 002 on 15/10/15.
//  Copyright © 2015年 002. All rights reserved.
//

import UIKit
import SnapKit

class VisitorView: UIView {
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        // 设置UI界面
        setupUI()
    }

    /// 可以防止从XIB入口进入，如果从XIB进入直接崩
    ///
    /// :param: aDecoder
    ///
    /// :returns:
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func starAnimate() {
        
        let animate = CABasicAnimation(keyPath: "transform.rotation")
        
        animate.byValue = 2 * M_PI
        animate.duration = 20
        animate.repeatCount = MAXFLOAT
        
        // When true, the animation is removed from the target layer’s animations once its active duration has passed. Defaults to true.
        animate.removedOnCompletion = false
        
        // 添加到需要执行动画的控件的图层
        iconView.layer.addAnimation(animate, forKey: nil)

    }
    
// MARK: - 懒加载控件
    // 房子
    private lazy var houseIconView : UIImageView = UIImageView.init(image: UIImage.init(named: "visitordiscover_feed_image_house"))
    
    // 动画图片
    private lazy var iconView : UIImageView = UIImageView.init(image: UIImage.init(named: "visitordiscover_feed_image_smallicon"))
    
    // 动画图片
    private lazy var maskIconView : UIImageView = UIImageView.init(image: UIImage.init(named: "visitordiscover_feed_mask_smallicon"))
    
    // 提示消息
    lazy var messageLabel : UILabel = UILabel.init(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", fontSize: 14, color: UIColor.darkGrayColor())
    
    // 注册按钮
    lazy var registerBtn : UIButton = UIButton.init(title: "注册", color: UIColor.orangeColor(), backImageName: "common_button_white_disable")
    
    // 登陆按钮
    lazy var loginBtn : UIButton = UIButton.init(title: "登陆", color: UIColor.darkGrayColor(), backImageName: "common_button_white_disable")
}


// MARK: - 设置UI界面
extension VisitorView {

    func setupUI() {
    
        // 添加控件
        self.addSubview(iconView)
        self.addSubview(maskIconView)
        self.addSubview(houseIconView)
        self.addSubview(messageLabel)
        self.addSubview(registerBtn)
        self.addSubview(loginBtn)
        
        // 设置背景色
        self.backgroundColor = UIColor.init(white: 237 / 255, alpha: 1)
        
        /// 各控件布局
        // 1> 动画图片
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        // 2> 房子
        houseIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView.snp_center)
        }
        // 3> 消息标签
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(houseIconView.snp_bottom).offset(50)
            make.width.equalTo(225)
        }
        // 4> 注册按钮
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            make.left.equalTo(messageLabel.snp_left).offset(0)
            make.width.equalTo(100)
            make.height.equalTo(30)
            
        }
        // 5> 登录按钮
            loginBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            make.right.equalTo(messageLabel.snp_right).offset(0)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        // 6> 遮罩
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.bottom.equalTo(registerBtn.snp_bottom)
        }
    }
    
}

// MARK: - 外部接口，用于设置不同控制器的房屋视图的特有信息
extension VisitorView {
    
    // 参数imageName注意是‘？’，因为不一定有值
    func setupVisitorView(imageName: String?,messageString : String) {
        
        // 如果图片为空，那么就是首页，直接返回,并且开启动画
        guard let imageName = imageName else {
            
            // 开启动画
            starAnimate()
            return
        }
        
        // 不是首页就重新设置首页图片
        iconView.image = UIImage.init(named: imageName)
        
        //隐藏房子
        houseIconView.hidden = true
        
        // 遮罩图片移动最底层
        self.sendSubviewToBack(maskIconView)
        
        // 设置提示信息
        messageLabel.text = messageString
        
    }

}
